package movie.test.user;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;

import movie.test.as.user.bean.AdminUser;
import movie.test.as.user.service.impl.AdminUserServiceImpl;
import movie.test.login.LoginController;

@RestController
@RequestMapping("/adminUserController")
public class AdminUserController {
	private static Logger logger = LoggerFactory
			.getLogger(AdminUserController.class);
  @Autowired	
  private AdminUserServiceImpl adminUserServiceImpl;
  @Autowired(required = false)

	//后台登陆
	@ResponseBody
	@RequestMapping(value={"/login"},method={RequestMethod.GET,RequestMethod.POST})
	public Object Adminlogin(@RequestBody JSONObject objs, HttpServletRequest request) {

		try{
			logger.debug("开始登录...");
		    Map map  = adminUserServiceImpl.loginUser(objs);
			logger.info("登录结束...");
		    Map<String,Object> result = new LinkedHashMap<String,Object>();
			if(map.containsKey("errorcode")){
				result.put("errorcodeInfo", map);
		     
			}else{
		       result.put("successInfo",map);
			}
			return result;
			}
		catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	//后台登陆
	@ResponseBody
	@RequestMapping(value={"/showAdminUser"},method={RequestMethod.GET,RequestMethod.POST})
	public Object showAdminUser(@RequestBody JSONObject objs, HttpServletRequest request) {

		try{
			logger.debug("开始显示账户...");
		    AdminUser admin = adminUserServiceImpl.showAdminUser(objs);
			logger.info("显示账户结束...");
		    LinkedHashMap result = new LinkedHashMap();
		    result.put("result", admin);
		    return result;
			}
		catch(Exception e){
			e.printStackTrace();
		}
		return null;
 }
	
	@ResponseBody
	@RequestMapping(value={"/findAllAdminList"},method={RequestMethod.GET,RequestMethod.POST})
	public Object findAllAdminList(@RequestBody JSONObject objs,HttpServletRequest request) {
		try {
			logger.debug("查询所有用户开始");
			PageInfo<Map<String,Object>>  listUsers = adminUserServiceImpl.findAllAdminList(objs);
			logger.info("查询所有用户结束");
			LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
			map.put("result", listUsers);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	@ResponseBody
	@RequestMapping(value={"/addAdminUser"},method={RequestMethod.GET,RequestMethod.POST})
	public Object addAdminUser(@RequestBody JSONObject objs,	HttpServletRequest request) {
		try {
			logger.debug("添加用户开始");
			Map<String, Object> map =  adminUserServiceImpl.addAdminUser(objs);
			logger.info("添加用户结束");
			Map<String, Object> result = new LinkedHashMap<String, Object>();
			if (map.containsKey("errorcode")) {
				result.put("errorResultInfo", map);
				return result;
				
			} else {
				result.put("successResultInfo", map);
				return result;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	
		return null;
	}
}